<?php
class ControllerDashboardSign extends Controller {
    public function index() {
        $this->load->language('dashboard/chart');

        $data['heading_title'] = $this->language->get('heading_title');

        $data['text_day'] = $this->language->get('text_day');
        $data['text_week'] = $this->language->get('text_week');
        $data['text_month'] = $this->language->get('text_month');
        $data['text_year'] = $this->language->get('text_year');
        $data['text_view'] = $this->language->get('text_view');

        $data['token'] = $this->session->data['token'];

        return $this->load->view('dashboard/sign.tpl', $data);
    }

    public function sign() {
        $this->load->language('dashboard/chart');

        $json = array();

        $this->load->model('report/sign');

        $json['order'] = array();
        $json['xaxis'] = array();

        $json['order']['label'] = '签到人数';
        $json['order']['data'] = array();

        if (isset($this->request->get['range'])) {
            $range = $this->request->get['range'];
        } else {
            $range = 'month';
        }

        switch ($range) {
            default:

            case 'week':
                $results = $this->model_report_sign->getTotalSignByWeek();

                foreach ($results as $key => $value) {
                    $json['order']['data'][] = array($key, $value['total']);
                }

                $date_start = strtotime('-' . date('w') . ' days');

                for ($i = 0; $i < 7; $i++) {
                    $date = date('Y-m-d', $date_start + ($i * 86400));

                    $json['xaxis'][] = array(date('w', strtotime($date)), date('D', strtotime($date)));
                }
                break;
            case 'month':
                $results = $this->model_report_sign->getTotalSignByMonth();

                foreach ($results as $key => $value) {
                    $json['order']['data'][] = array($key, $value['total']);
                }


                for ($i = 1; $i <= date('t'); $i++) {
                    $date = date('Y') . '-' . date('m') . '-' . $i;

                    $json['xaxis'][] = array(date('j', strtotime($date)), date('d', strtotime($date)));
                }
                break;
            case 'year':
                $results = $this->model_report_sign->getTotalSignByYear();

                foreach ($results as $key => $value) {
                    $json['order']['data'][] = array($key, $value['total']);
                }



                for ($i = 1; $i <= 12; $i++) {
                    $json['xaxis'][] = array($i, date('M', mktime(0, 0, 0, $i)));
                }
                break;
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}